require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'
require 'win32ole'
require 'win32/process'

class TC_BlogPageUnPublish_test < Test::Unit::TestCase
      def startup
      $GroupName= 'group1'
      $GroupDescription = 'This is the first group for creating blogs'
      $SectionName = 'BloG1'
      $AppKey ='BlogAppKey'
      $Owners = 'admin'
      $FeedUrl = 'http://www.greatdeccan.com/rss'
      $SiteUrl = 'www.greatdeccan.com'
      $PostTitle= 'Post1'
      $PostDescription = 'This is the first blog post'
      $Summary = 'This is the summary for the blog post'
      $Tags = 'first,automated '
      $Date ='June 11 2008 02:50 AM'
      $PostName = 'Post,Url'
      $FileUrl ='http://www.freefoto.com/images/15/19/15_19_3---Tree--Sunrise--Northumberland_web.jpg?&k=Tree%2C+Sunrise%2C+Northumberland'
      @@ie = Watir::IE.start($SITE_URL)
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
    def test_0000
        require 'win32/process'
        $pid = Process.create(
        :app_name      => 'ruby ../resources/clickers.rb',
        :creation_flags  => Process::DETACHED_PROCESS
        ).process_id
        
    end  
    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
   def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
    end
    
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
    end
    
    def test_07_verifyControlPanelLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
    end
    
    def test_08_navigateToBlogAdministration
        ie.goto($SITE_URL + 'controlpanel/BlogAdmin/')
        assert(ie.contains_text('Blog Administration'))      
    end  
    def test_09_verifyLinksExist
      # Make sure that all of the links exist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Blog Administration').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
    end
     def test_10_navigateToCreateGroup 
        ie.goto($SITE_URL +'controlpanel/BlogAdmin/Groups.aspx')
        assert(ie.button(:value , 'Add New Group').exists?)
        ie.button(:value , 'Add New Group').click
        assert(ie.text_field(:id,/GroupName$/).exists?)
        ie.text_field(:id,/GroupName$/).set($GroupName)
        assert(ie.text_field(:id,/GroupDesc$/).exists?)
        ie.text_field(:id,/GroupDesc$/).set($GroupDescription)
        assert(ie.button(:id,/SaveButton$/).exists?)
        ie.button(:id,/SaveButton$/).click
    end
    def test_11_CreateBlog
        ie.goto($SITE_URL +'controlpanel/BlogAdmin/sectionedit.aspx')
        assert(ie.text_field(:id,/SectionName$/).exists?)
        ie.text_field(:id,/SectionName$/).set($SectionName)
        assert(ie.text_field(:id,/AppKey$/).exists?)
        ie.text_field(:id,/AppKey$/).set($AppKey)
        assert(ie.text_field(:id,/Owners_ctl00$/).exists?)
        ie.text_field(:id,/Owners_ctl00$/).value =$Owners
        assert(ie.select_list(:id,/AdminGroupList$/).exists?)
        ie.select_list(:id,/AdminGroupList$/).select($GroupName)
        assert(ie.button(:id,/SaveButton$/).exists?)
        ie.button(:id,/SaveButton$/).click
    end
    
    def test_12_navigateToNewPage
       sleep 3
        ie.goto($SITE_URL+'controlpanel/BlogAdmin/Blogs.aspx')
        assert(ie.link(:text,'All').exists?)
        ie.link(:text,'All').click
        assert(ie.table(:id,'BlogLists').exists?)
        table1 = ie.table(:id,'BlogLists')
        rows = table1.row_count
        rowcount1 =1
        while rowcount1<=rows
            if (table1[rowcount1][1].link(:text,$SectionName).exists?)
               table1[rowcount1][5].button(:value ,'Manage').click
                break
             end   
         rowcount1=rowcount1+1
        end
        sleep 2
        ie.goto($SITE_URL + 'controlpanel/blogs/posteditor.aspx?SelectedNavItem=NewPage&bpt=2')
        sleep 2
        sleep 2
        assert(ie.text_field(:id,/CreatePost_ctl03_PostSubject$/).exists?)
        ie.text_field(:id,/CreatePost_ctl03_PostSubject$/).set($PostTitle)
        sleep 2
        ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_CreatePost_ctl03_PostBody_ctl00_Editor').execCommand('mceSetContent', false,'"+$PostDescription+"')")
        assert(ie.text_field(:id,/CreatePost_ctl03_PostTags_ctl00_Tags$/).exists?)
        ie.text_field(:id,/CreatePost_ctl03_PostTags_ctl00_Tags$/).value=$Tags
        
        assert(ie.button(:value,'Select File...').exists?)
        ie.button(:value,'Select File...').click
        
        sleep 5
        assert(ie.frame(:index,2).radio(:id,/LinkToUrl$/).exists?)
        ie.frame(:index,2).radio(:id,/LinkToUrl$/).set
        assert(ie.frame(:index,2).text_field(:id,/UploadAttachment_Url$/).exists?)
        ie.frame(:index,2).text_field(:id,/UploadAttachment_Url$/).set($FileUrl)
        assert(ie.frame(:index,2).link(:id,/UploadAttachment_Save$/).exists?)
        ie.frame(:index,2).link(:id,/UploadAttachment_Save$/).click
        # Comments & Feedback
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_CreatePost_ctl03_EditorTabs.SelectTab(ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_CreatePost_ctl03_EditorTabs.GetTabAtIndex(1))")
        # Reader Comments
        assert(ie.radio(:id,/ynEnableReplies_Yes$/).exists?)
        assert(ie.radio(:id,/ynEnableReplies_No$/).exists?)
        ie.radio(:id,/ynEnableReplies_Yes$/).set
        assert(ie.select_list(:id,/ModerationDDL$/).exists?)
        ie.select_list(:id,/ModerationDDL$/).select('Anonymous comments must be approved')
        assert(ie.radio(:id,/ynEnableRatings_Yes$/).exists?)
        assert(ie.radio(:id,/ynEnableRatings_No$/).exists?)
        ie.radio(:id,/ynEnableRatings_Yes$/).set
        # Email Notifications
        assert(ie.select_list(:id,/NotificationType$/).exists?)
        ie.select_list(:id,/NotificationType$/).select('Do not send email')
        # Reader Trackbacks
        assert(ie.radio(:id,/ynEnableTrackbacks_Yes$/).exists?)
        assert(ie.radio(:id,/ynEnableTrackbacks_No$/).exists?)
        ie.radio(:id,/ynEnableTrackbacks_No$/).set
        # Publication Settings
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_CreatePost_ctl03_EditorTabs.SelectTab(ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_CreatePost_ctl03_EditorTabs.GetTabAtIndex(2))")
        # Post Summary
        assert(ie.radio(:id,/ynSyndicateExcerpt_Yes$/).exists?)
        assert(ie.radio(:id,/ynSyndicateExcerpt_No$/).exists?)
        ie.radio(:id,/ynSyndicateExcerpt_No$/).set
        sleep 2
        assert(ie.text_field(:id,/CreatePost_ctl03_postExcerpt$/).exists?)
        sleep 2
        ie.text_field(:id,/CreatePost_ctl03_postExcerpt$/).set($Summary)
        # Publication Date
        assert(ie.text_field(:id,/DatePicker_text$/).exists?)
        ie.text_field(:id,/DatePicker_text$/).set($Date)
        # Advanced Options
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_CreatePost_ctl03_EditorTabs.SelectTab(ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_CreatePost_ctl03_EditorTabs.GetTabAtIndex(3))")
        # Display Post
        assert(ie.radio(:id,/ynCommunity_Yes$/).exists?)
        assert(ie.radio(:id,/ynCommunity_No$/).exists?)
        ie.radio(:id,/ynCommunity_No$/).set
        assert(ie.radio(:id,/ynAggregatePost_Yes$/).exists?)
        assert(ie.radio(:id,/ynAggregatePost_No$/).exists?)
        ie.radio(:id,/ynAggregatePost_No$/).set
        # Search Engine Optimization
        assert(ie.text_field(:id,/CreatePost_ctl03_postName$/).exists?)
        ie.text_field(:id,/CreatePost_ctl03_postName$/).set($PostName)
        # Cross Posting
        assert(ie.radio(:id,/ynEnableCrossPosting_Yes$/).exists?)
        assert(ie.radio(:id,/ynEnableCrossPosting_No$/).exists?)
        ie.radio(:id,/ynEnableCrossPosting_No$/).set
        # Preview
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_CreatePost_ctl03_EditorTabs.SelectTab(ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_CreatePost_ctl03_EditorTabs.GetTabAtIndex(4))")
        # UnPublish Page
        assert(ie.link(:id,/SaveButton$/).exists?)
        ie.link(:id,/SaveButton$/).click
      end  
      
   def test_13_navigateToDeletePost
        assert(ie.table(:id,'CommentListing').exists?)
        table1 = ie.table(:id, 'CommentListing')
        rowcount = table1.row_count
        
        rowcount1 =1
        ok = 0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$PostTitle).exists?)
              ok =1
               sleep 5
               table1[rowcount1][5].button(:id, /DeleteButton$/).click
               break
             end   
         rowcount1=rowcount1+1
        end
      end
      
       def test_14_navigateToDeleteBlog
        ie.goto($SITE_URL+'controlpanel/BlogAdmin/Blogs.aspx')
        assert(ie.table(:id,'BlogLists').exists?)
        table1 = ie.table(:id,'BlogLists')
        rowcount = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$SectionName).exists?)
              ok =1
               sleep 5
               table1[rowcount1][5].button(:value,'Delete' ).click
               break
            end   
            rowcount1=rowcount1+1
        end
      end
      
      def test_15_navigateToDeleteBlogGroup
        ie.goto($SITE_URL+'controlpanel/BlogAdmin/Groups.aspx')
        assert(ie.table(:id,'GroupLists').exists?)
        table2 = ie.table(:id,'GroupLists')
        rows = table2.row_count
        rowcount2 =1
        ok = 0
        while rowcount2<=rows
            if (table2[rowcount2][1].link(:text,$GroupName).exists?)
              ok =1
               sleep 5
               table2[rowcount2][3].button(:value,'Delete' ).click
               break
            end   
         rowcount2=rowcount2+1
        end
    end
      
      def test_99_Exit
        ie.close
      end

end  